/*
 * Created by JFormDesigner on Tue Jul 09 09:48:06 CST 2024
 */

package com.hongyan.dialog;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.swing.*;
import javax.swing.border.*;

import com.hongyan.bean.Class;
import com.hongyan.bean.Log;
import com.hongyan.bean.Student;
import com.hongyan.config.MainFrameConfig;
import com.hongyan.dao.ClassDao;
import com.hongyan.dao.LogDao;
import com.hongyan.dao.S_infoDao;


/**
 * @author admin
 */
public class SwingAdminUserDialog extends JDialog {

    private static String[] sex = {"男", "女"};

    private static ArrayList<String> college = new ArrayList<>(Arrays.asList((("建筑学院\n" + "信息科学技术学院\n" + "五道口金融学院\n" + "新闻与传播学院\n" + "法学院\n" + "社会科学学院\n" + "航天航空学院\n" + "机械工程学院\n" + "人文学院\n" + "马克思主义学院\n" + "环境学院\n" + "公共管理学院").split("\n"))));

    private String AoU;

    public SwingAdminUserDialog(Window owner) {
        super(owner);
        initComponents();
        initData();
    }

    private void initData() {
        textField1.setVisible(false);
        label1.setVisible(false);
        List<Class> classList = ClassDao.getClassList();
        Object[] array = classList.stream().map(Class::getC_name).toArray();
        comboBox3.setModel(new DefaultComboBoxModel<>(array));
        comboBox1.setModel(new DefaultComboBoxModel<>(sex));
        comboBox2.setModel(new DefaultComboBoxModel<>(college.toArray()));
        cancelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });
        okButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {



                if (textField2.getText().isEmpty()) {
                    JOptionPane.showMessageDialog(null, "学生姓名不能为空");
                    return;
                }
                if (textField5.getText().isEmpty()) {
                    JOptionPane.showMessageDialog(null, "学生联系方式不能为空");
                    return;
                }
                if (textField7.getText().isEmpty()) {
                    JOptionPane.showMessageDialog(null, "学生联系方式不能为空");
                    return;
                }


                Student Student = new Student();
                Student.setS_id(textField1.getText());
                Student.setS_name(textField2.getText());
                Student.setC_id(classList.get(comboBox3.getSelectedIndex()).getC_id());
                Student.setSex(sex[comboBox1.getSelectedIndex()]);
                Student.setContact(textField5.getText());
                Student.setCollege(college.get(comboBox2.getSelectedIndex()));
                Student.setPassword(textField7.getText());
                int i = 0;
                Log log = new Log();
                log.setL_id(MainFrameConfig.getSnowflakeUtils().getId());
                log.setL_name("用户操作");
                log.setRecord_time(new Timestamp(System.currentTimeMillis()));
                if (AoU != "u") {
                    Student.setS_id(System.currentTimeMillis() + "");
                    log.setContent(String.format("管理员:%s添加了用户%s", MainFrameConfig.getId(), Student.getS_id()));
                    LogDao.addLog(log);
                    i = S_infoDao.insertStudent(Student);
                } else {
                    log.setContent(String.format("管理员:%s修改了用户%s", MainFrameConfig.getId(), Student.getS_id()));
                    LogDao.addLog(log);
                    i = S_infoDao.updateStudent(Student);
                }
                if (i > 0) {
                    JOptionPane.showMessageDialog(null, "添加成功");
                } else {
                    JOptionPane.showMessageDialog(null, "添加失败");
                }
                dispose();
            }
        });
    }

    public void updateSys(Student student, String a) {
        textField1.setText(student.getS_id());
        Student uStudent = S_infoDao.findS_infoById(student.getS_id());
        AoU = a;

        textField2.setText(uStudent.getS_name());
        comboBox3.setSelectedIndex(0);
        if ("男".equals(uStudent.getSex())) {
            comboBox1.setSelectedIndex(0);
        } else {
            comboBox1.setSelectedIndex(1);
        }
        textField5.setText(uStudent.getContact());
        comboBox2.setSelectedIndex(0);
        textField7.setText(uStudent.getPassword());
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        dialogPane = new JPanel();
        contentPanel = new JPanel();
        label1 = new JLabel();
        textField1 = new JTextField();
        label2 = new JLabel();
        label3 = new JLabel();
        label4 = new JLabel();
        label5 = new JLabel();
        textField2 = new JTextField();
        textField5 = new JTextField();
        textField7 = new JTextField();
        label6 = new JLabel();
        buttonBar = new JPanel();
        okButton = new JButton();
        cancelButton = new JButton();
        label7 = new JLabel();
        comboBox1 = new JComboBox();
        comboBox3 = new JComboBox();
        comboBox2 = new JComboBox();

        //======== this ========
        setTitle("\u6dfb\u52a0\u5b66\u751f\u4fe1\u606f");
        Container contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout());

        //======== dialogPane ========
        {
            dialogPane.setBorder(new EmptyBorder(12, 12, 12, 12));
            dialogPane.setLayout(new BorderLayout());

            //======== contentPanel ========
            {
                contentPanel.setLayout(null);

                //---- label1 ----
                label1.setText("\u5b66\u53f7\uff1a");
                contentPanel.add(label1);
                label1.setBounds(new Rectangle(new Point(65, 5), label1.getPreferredSize()));
                contentPanel.add(textField1);
                textField1.setBounds(110, 0, 185, textField1.getPreferredSize().height);

                //---- label2 ----
                label2.setText("\u59d3\u540d\uff1a");
                contentPanel.add(label2);
                label2.setBounds(65, 45, 65, 17);

                //---- label3 ----
                label3.setText("\u73ed\u7ea7\u53f7\uff1a");
                contentPanel.add(label3);
                label3.setBounds(55, 85, 70, 17);

                //---- label4 ----
                label4.setText("\u6027\u522b\uff1a");
                contentPanel.add(label4);
                label4.setBounds(65, 120, 70, 17);

                //---- label5 ----
                label5.setText("\u8054\u7cfb\u65b9\u5f0f\uff1a");
                contentPanel.add(label5);
                label5.setBounds(40, 155, 70, 17);
                contentPanel.add(textField2);
                textField2.setBounds(110, 40, 185, 23);
                contentPanel.add(textField5);
                textField5.setBounds(110, 150, 185, 23);
                contentPanel.add(textField7);
                textField7.setBounds(110, 225, 185, 23);

                //---- label6 ----
                label6.setText("\u5b66\u9662\uff1a");
                contentPanel.add(label6);
                label6.setBounds(65, 190, 70, 17);

                //======== buttonBar ========
                {
                    buttonBar.setBorder(new EmptyBorder(12, 0, 0, 0));
                    buttonBar.setLayout(new GridBagLayout());
                    ((GridBagLayout)buttonBar.getLayout()).columnWidths = new int[] {0, 85, 80};
                    ((GridBagLayout)buttonBar.getLayout()).columnWeights = new double[] {1.0, 0.0, 0.0};

                    //---- okButton ----
                    okButton.setText("\u6dfb\u52a0");
                    buttonBar.add(okButton, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
                        GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                        new Insets(0, 0, 0, 5), 0, 0));

                    //---- cancelButton ----
                    cancelButton.setText("\u53d6\u6d88");
                    buttonBar.add(cancelButton, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0,
                        GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                        new Insets(0, 0, 0, 0), 0, 0));
                }
                contentPanel.add(buttonBar);
                buttonBar.setBounds(0, 255, 374, buttonBar.getPreferredSize().height);

                //---- label7 ----
                label7.setText("\u5bc6\u7801\uff1a");
                contentPanel.add(label7);
                label7.setBounds(65, 230, 70, 17);
                contentPanel.add(comboBox1);
                comboBox1.setBounds(110, 115, 185, comboBox1.getPreferredSize().height);
                contentPanel.add(comboBox3);
                comboBox3.setBounds(110, 80, 185, 23);
                contentPanel.add(comboBox2);
                comboBox2.setBounds(110, 190, 185, 23);

                {
                    // compute preferred size
                    Dimension preferredSize = new Dimension();
                    for(int i = 0; i < contentPanel.getComponentCount(); i++) {
                        Rectangle bounds = contentPanel.getComponent(i).getBounds();
                        preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                        preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                    }
                    Insets insets = contentPanel.getInsets();
                    preferredSize.width += insets.right;
                    preferredSize.height += insets.bottom;
                    contentPanel.setMinimumSize(preferredSize);
                    contentPanel.setPreferredSize(preferredSize);
                }
            }
            dialogPane.add(contentPanel, BorderLayout.CENTER);
        }
        contentPane.add(dialogPane, BorderLayout.CENTER);
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JPanel dialogPane;
    private JPanel contentPanel;
    private JLabel label1;
    private JTextField textField1;
    private JLabel label2;
    private JLabel label3;
    private JLabel label4;
    private JLabel label5;
    private JTextField textField2;
    private JTextField textField5;
    private JTextField textField7;
    private JLabel label6;
    private JPanel buttonBar;
    private JButton okButton;
    private JButton cancelButton;
    private JLabel label7;
    private JComboBox comboBox1;
    private JComboBox comboBox3;
    private JComboBox comboBox2;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
